package com.cn.yang.service.impl;

import com.cn.yang.dao.ContactUsDao;
import com.cn.yang.dao.impl.ContactUsDaoImpl;
import com.cn.yang.model.contactUs.ContactUsModel;
import com.cn.yang.service.ContactUsService;
import redis.clients.jedis.Jedis;

import java.util.List;

public class ContactUsServiceImpl implements ContactUsService {
    //查询 ContactUs
    @Override
    public ContactUsModel findContactUs(ContactUsModel model) {
        ContactUsDao dao = new ContactUsDaoImpl();
        return dao.findContactUs(model);
    }
    public ContactUsModel findContactUs() {
        ContactUsDao dao = new ContactUsDaoImpl();
        Jedis jedis = new Jedis();
        String id = jedis.hget("findContactUs","id");
        ContactUsModel model = new ContactUsModel();
        if (id!=null){
            model.setId(Integer.valueOf(jedis.hget("findContactUs","id")));
            model.setLocation(jedis.hget("findContactUs","location"));
            model.setContactphone(jedis.hget("findContactUs","contactphone"));
            model.setOrderphone(jedis.hget("findContactUs","orderphone"));
            model.setEmail(jedis.hget("findContactUs","email"));
            model.setNet(jedis.hget("findContactUs","net"));
            model.setLine(jedis.hget("findContactUs","line"));
            model.setWechat(jedis.hget("findContactUs","wechat"));
            model.setTwitter(jedis.hget("findContactUs","twitter"));
        }else {
            model = dao.findContactUs();
            jedis.hset("findContactUs","id", String.valueOf(model.getId()));
            jedis.hset("findContactUs","location", model.getLocation());
            jedis.hset("findContactUs","contactphone", model.getContactphone());
            jedis.hset("findContactUs","orderphone", model.getOrderphone());
            jedis.hset("findContactUs","email", model.getEmail());
            jedis.hset("findContactUs","net", model.getContactphone());
            jedis.hset("findContactUs","line", model.getLine());
            jedis.hset("findContactUs","wechat", model.getWechat());
            jedis.hset("findContactUs","twitter", model.getTwitter());
        }
        return model;
    }
//查询总条数
    @Override
    public int countContactUs() {
        ContactUsDao dao = new ContactUsDaoImpl();
        Jedis jedis = new Jedis();
        String count = jedis.get("countContactUs");
        if (count==null){
            int a = dao.countContactUs();
            jedis.set("countContactUs", String.valueOf(a));
            return a;
        }else {
            return Integer.parseInt(count);
        }
    }
//根据ID查询
    @Override
    public ContactUsModel selectById(Integer id) {
        ContactUsDao dao = new ContactUsDaoImpl();
        // ContactUsModel model = new ContactUsModel();
        // Jedis jedis = new Jedis();
        return dao.selectById(id);
    }
//更新数据
    @Override
    public int updateContactUs(ContactUsModel model) {
        ContactUsDao dao = new ContactUsDaoImpl();
        Jedis jedis = new Jedis();
        jedis.del("findContactUs");
        return dao.updateContactUs(model);
    }
}
